package com.keyinong.jiashiyuan.view;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.Toast;

import com.keyinong.jiashiyuan.R;

public class JiashiyuanActivity extends FragmentActivity {
	DingdanFramgment dingdanFramgment;
	ShangjiaFramgment shangjiaFramgment;
	MyFramgment myFramgment;
	
	RadioButton [] rbArray= new RadioButton[3];
	
	Fragment [] fragmentArray = null;
	/**
	 * 当前显示的fragment
	 */
	int currentIndex = 0;
	/**
	 * 选中的button,显示下一个fragment
	 */
	int selectedIndex;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try {
			setContentView(R.layout.activity_jiashiyuan);
			setupView();
			addListener();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}

	private void addListener() {
		MyButtonListener myButtonListener = new MyButtonListener();
		for (int i = 0; i < rbArray.length; i++) {
			rbArray[i].setOnClickListener(myButtonListener);
		}
		
	}

	private void setupView() {
		rbArray[0] = (RadioButton)findViewById(R.id.rb_dingdan);
		rbArray[1] = (RadioButton)findViewById(R.id.rb_shangjia);
		rbArray[2] = (RadioButton)findViewById(R.id.rb_my);
		rbArray[0].setSelected(true);
		
		dingdanFramgment = new DingdanFramgment();
		shangjiaFramgment = new ShangjiaFramgment();
		myFramgment = new MyFramgment();
		fragmentArray = new Fragment[] {dingdanFramgment,shangjiaFramgment,myFramgment};
		
		//一开始，显示第一个fragment
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction transaction=fragmentManager.beginTransaction();
		transaction.add(R.id.fram_jiashiyuan, dingdanFramgment);
		transaction.show(dingdanFramgment);
		transaction.commit();
	}
	
	class MyButtonListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			try {
				switch (v.getId()) {
				case R.id.rb_dingdan:
					selectedIndex = 0;
					break;
				case R.id.rb_shangjia:
					selectedIndex = 1;
					break;
				case R.id.rb_my:
					selectedIndex = 2;
					break;
				}

				// 判断单击是不是当前的
				if (selectedIndex != currentIndex) {
					// 不是当前的
					FragmentTransaction transaction = getSupportFragmentManager()
							.beginTransaction();
					// 当前hide
					transaction.hide(fragmentArray[currentIndex]);
					// show你选中

					if (!fragmentArray[selectedIndex].isAdded()) {
						// 以前没添加过
						transaction.add(R.id.fram_jiashiyuan,
								fragmentArray[selectedIndex]);
					}
					// 事务
					transaction.show(fragmentArray[selectedIndex]);
					transaction.commit();

					rbArray[currentIndex].setSelected(false);
					rbArray[selectedIndex].setSelected(true);
					currentIndex = selectedIndex;

				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}
	private long exitTime = 0;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){   
	        if((System.currentTimeMillis()-exitTime) > 2000){  
	            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();                                
	            exitTime = System.currentTimeMillis();   
	        } else {
	            finish();
	            System.exit(0);
	        }
	        return true;   
	    }
	    return super.onKeyDown(keyCode, event);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.jiashiyuan, menu);
		return true;
	}

}
